import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App.vue'
import VueRouter from "vue-router";
import routers from "~/views/router/index";
import echarts from 'echarts';
import Common from "~/components/Common";
import store from './store';
import moment from 'moment'
import Axios from "axios";
import '~/utils/LibFlexible';


Vue.prototype.$echarts = echarts;
Vue.prototype.$common = Common;
Vue.prototype.$moment = moment;
Vue.prototype.$http = Axios;

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由配置
const RouterConfig = {
  mode: 'history',
  routes: routers,
  base: '/group', // 配置单页应用的基路径
};

const router = new VueRouter(RouterConfig);
router.beforeEach((to, from, next) => {
  let isLogin = window.sessionStorage.getItem("token");
  if (to.path == "/login") {
      next();
  } else {
      if (!isLogin || isLogin === null || isLogin === "") {
          next("/login");
      } else {
          next();
      }
  }
})
// import 'amfe-flexible';
Vue.use(VueRouter)
Vue.use(ElementUI)

Axios.interceptors.request.use(
  config => {
    // 判断是否存在token，如果存在的话，则每个http header都加上token
    let token = window.sessionStorage.getItem('token')
    if (!config.headers.hasOwnProperty('token') && token) {
      config.headers.token = token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  });
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
